﻿using System;
using Dexter.CallContext;
using Dexter.Logger.Contracts;
using Quartz;

namespace Dexter.Scheduler.Quartz.Factory {
	internal class QuartzJobDetailFactory {
		public static Tuple<JobDetail, Trigger> GetJobFromFrequently (IFrequentlyJob job, IDexterCall dexterCall, ILoggerFactory loggerFactory ) {
			var wrapper = new QuartzJobWrapper ( job ,dexterCall, loggerFactory.Create ( typeof(QuartzJobWrapper) ));

			JobDetail jobDetail = new JobDetail ( job.UniqueId , job.GroupName , wrapper.GetType ( ) );
			jobDetail.Volatile = job.IsVolatile;
			jobDetail.Durable = true;

			var trigger = TriggerUtils.MakeMinutelyTrigger (job.Minutes );
			trigger.Name = job.UniqueId + "_Trigger";
			trigger.Group = job.GroupName;

			return new Tuple<JobDetail, Trigger> ( jobDetail, trigger );
		}

		public static Tuple<JobDetail, Trigger> GetJobFromCron ( ICronJob job, IDexterCall dexterCall, ILoggerFactory loggerFactory ) {
			var wrapper = new QuartzJobWrapper ( job , dexterCall , loggerFactory.Create ( typeof ( QuartzJobWrapper ) ) );

			JobDetail jobDetail = new JobDetail ( job.UniqueId, job.GroupName, wrapper.GetType ( ) );
			jobDetail.Volatile = job.IsVolatile;
			jobDetail.Durable = true;

			var trigger = new CronTrigger ( job.UniqueId + "_Trigger", job.GroupName, job.CronPatter );

			return new Tuple <JobDetail , Trigger> ( jobDetail , trigger );
		}
	}
}
